package com.hrma.controller;

import com.github.pagehelper.Page;
import com.hrma.entity.*;
import com.hrma.services.RewardService;
import com.hrma.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reward")
@Slf4j
public class RewardController {

    private final RewardService rewardService;

    public RewardController(RewardService rewardService) {
        this.rewardService = rewardService;
    }

    @PostMapping("/add")
    public Result<Reward> add(@Validated @RequestBody Reward data) {
        log.debug("收到add请求，请求参数：{}", data);
        return Result.ok(rewardService.add(data));
    }

    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable int id) {
        log.debug("收到delete请求，请求参数：{}", id);
        rewardService.delete(id);
        return Result.ok(null);
    }

    @PatchMapping("/update")
    public Result<?> update(@RequestBody Reward data) {
        log.debug("收到update请求，请求参数：{}", data);
        rewardService.update(data);
        return Result.ok(null);
    }

    @GetMapping("/select")
    public Result<List<SelectResult>> select() {
        log.debug("收到select请求！");
        return Result.ok(rewardService.select());

    }

    @PostMapping("/list")
    public Result<ListWithCount<Reward>> getList(@RequestBody RewardDTO data) {
        log.debug("收到list请求，请求参数：{}", data);
        Page<Reward> res = rewardService.getList(data);

        return Result.ok(new ListWithCount<>(res.getTotal(), res));
    }
}
